﻿using DataAccess.DAL;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;


namespace Admin.Controllers
{
    public class PromoterController : Controller
    {
        //
        // GET: /Promoter/
        public ActionResult Index()
        {
            PromoterDAL dal = new PromoterDAL();
            int count = 0;
            var list = dal.ListPage("", "", 20, 1, out count);
            return View(list);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(Promoter model)
        {
            if (model != null)
            {
                model.CreatTime = DateTime.Now;
                model.UpdateTime = model.CreatTime;
                model.Grade = 0;
                model.IsDel = false;
                model.IsEnabled = true;
                model.OpenId = "";
                model.Promoter_One_Ratio = 0;
                model.Promoter_Two_Ratio = 0;
                model.Promoter_One = 0;
                model.Promoter_Two = 0;

                model.Password = XYD.Common.EncodeSecurity.EncryptString(model.Password);
                PromoterDAL dal = new PromoterDAL();
                if (dal.Insert(model))
                {
                    OrdersDAL odal = new OrdersDAL();
                    odal.CreateOrderTable(model.Id);
                    return Content("<script> alert('成功');window.location.href='/promoter' </script>");


                }


            }

            return Content("<script> alert('失败') </script>");



        }


        public ActionResult Edit(int id)
        {
            Promoter model = new Promoter();
            if (id > 0)
            {
                PromoterDAL dal = new PromoterDAL();
                model = dal.Get(id);
                //model.Password = "";

            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(Promoter model)
        {

            if (model != null && model.Id > 0)
            {
                PromoterDAL dal = new PromoterDAL();
                var oldmodel = dal.Get(model.Id);
                if (oldmodel == null)
                    return Content("<script> alert('信息不存在') </script>");

                oldmodel.Username = model.Username;
                oldmodel.StartTime = model.StartTime;
                oldmodel.EndTime = model.EndTime;
                oldmodel.ProductNum = model.ProductNum;

                oldmodel.UpdateTime = DateTime.Now;



                if (dal.Update(oldmodel))
                {
                    return Content("<script> alert('修改成功');window.location.href='/promoter' </script>");
                }


            }

            return Content("<script> alert('失败') </script>");


        }

        [HttpGet]
        public ActionResult Delete(int id)
        {
            PromoterDAL dal = new PromoterDAL();
            if (dal.Delete(id))
            {
                return Content("<script> alert('删除成功');window.location.href='/promoter' </script>");
            }

            return Content("<script> alert('修改成功') </script>");
        }


        [HttpPost]
        public ActionResult UpdatePW(int proid, string password1, string password2)
        {
            if (proid > 0 && !string.IsNullOrEmpty(password1) && password1 == password2)
            {
                PromoterDAL dal = new PromoterDAL();
                var oldmodel = dal.Get(proid);

                oldmodel.UpdateTime = DateTime.Now;
                oldmodel.Password = XYD.Common.EncodeSecurity.EncryptString(password1);
                if (dal.Update(oldmodel))
                {
                    return Content("<script> alert('修改密码成功');window.location.href='/promoter' </script>");
                }
            }
            return Content("<script> alert('失败') </script>");
        }

        [HttpPost]
        public ActionResult addRobot(int pid, int robotNum)
        {
            if (pid > 0 && robotNum > 0)
            {
                //PromoterDAL dal = new PromoterDAL();
                //var oldmodel = dal.Get(proid);

                //oldmodel.UpdateTime = DateTime.Now;
                //oldmodel.Password = XYD.Common.EncodeSecurity.EncryptString(password1);
                //if (dal.Update(oldmodel))
                RobotDAL dal = new RobotDAL();

                List<Robot> list = new List<Robot>();
                for (int i = 0; i < robotNum; i++)
                {
                    list.Add(new Robot()
                    {
                        activation = Guid.NewGuid().ToString(),
                        name = "",
                        account = "",
                        password = "",
                        add_date = DateTime.Now,
                        last_date = DateTime.Now,
                        state = 1,
                        is_delete = 0,
                        start_date = DateTime.Now,
                        end_date = DateTime.Now.AddYears(1),
                        promoter_id = pid,
                        reg_date = DateTime.Now,
                        is_login = 0
                    });
                }

                if (dal.Insert(list))
                {
                    return Content("<script> alert('成功');window.location.href='/promoter' </script>");
                }
            }
            return Content("<script> alert('失败') </script>");
        }
    }
}